//
//  MovieViewController.m
//  UI_TabBarTest1(豆瓣)
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "MovieViewController.h"
#import "Movie.h"
#import "MovieListTableViewCell.h"
#import "MovieShowViewController.h"
#import "MovieCollectionViewCell.h"
@interface MovieViewController ()
<
UITableViewDataSource,
UITableViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegate
>

@property (nonatomic,retain)NSMutableArray *movieArray;
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)UICollectionView *collectionView;

@end

@implementation MovieViewController

-(void)dealloc{
    [_collectionView release];
    [_tableView release];
    [_movieArray release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self config];
    [self setNavi];
    [self handleData];
    [self createTableView];
    [self createCollectionView];
    [self createChangeButton];
}
-(void)config{
    self.movieArray = [NSMutableArray arrayWithCapacity:5];
}

-(void)createChangeButton{
    UIBarButtonItem *changeView = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btn_nav_collection"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(changeView:)];
    
    self.navigationItem.rightBarButtonItem = changeView;
}

-(void)changeView:(UIBarButtonItem *)changeView{
    if (self.collectionView.hidden == YES){
        self.collectionView.hidden = NO;
        self.tableView.hidden = YES;
        changeView.image = [[UIImage imageNamed:@"btn_nav_list"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    } else {
        self.tableView.hidden = NO;
        self.collectionView.hidden = YES;
        changeView.image = [[UIImage imageNamed:@"btn_nav_collection"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
}

-(void)setNavi{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:0];
    self.title = @"电影";
}



-(void)handleData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"movielist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSArray *array = [dictionary valueForKey:@"result"];
    for (NSDictionary *dic in array){
        Movie *movie = [[Movie alloc] init];
        [movie setValuesForKeysWithDictionary:dic];
        [self.movieArray addObject:movie];
    }
}

-(void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, self.view.frame.size.width - 40, self.view.frame.size.height - 49) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 200.0;
    
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[MovieListTableViewCell class] forCellReuseIdentifier:@"movieListCellReuse"];
    [_tableView release];
}

-(void)createCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(120, 200);
    flowLayout.minimumInteritemSpacing = 13.0;
    flowLayout.minimumLineSpacing = 13.0;
    flowLayout.sectionInset = UIEdgeInsetsMake(13.0, 13.0, 13.0, 13.0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:@"movieListCollectionCellReuse"];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.movieArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"movieListCollectionCellReuse" forIndexPath:indexPath];
    cell.movie = [self.movieArray objectAtIndex:indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MovieShowViewController *movieShowVC = [[MovieShowViewController alloc] init];
    movieShowVC.movie = [self.movieArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:movieShowVC animated:YES];
    [movieShowVC release];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.movieArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieListCellReuse" forIndexPath:indexPath];
    cell.movie = [self.movieArray objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieShowViewController *movieShowVC = [[MovieShowViewController alloc] init];
    movieShowVC.movie = [self.movieArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:movieShowVC animated:YES];
    [movieShowVC release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
